<?php

use App\Admin\Controller\Base\RouteController;
use App\Common\Enums\Base\RouteEnumType;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Table;

Html::create('路由列表');

$table = Table::create(TP::route()->to([RouteController::class, 'listsData'])->url(), 'route');

$table->addColumns(
    Table\Column::selection(),
    Table\Column::normal('标题', 'title'),
    Table\Column::normal('类型', 'type')->showTag(RouteEnumType::tagMapping()),
    Table\Column::normal('路由地址', 'route'),
    Table\Column::normal('路由权重', 'weigh'),
    Table\Column::normal('图标', 'icon')->setFormat('<el-icon><component :is="icon"></component></el-icon>'),
);

$component = include __DIR__ . '/edit.sc.php';

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增路由')->setConfig(['width' => '500px'])->setContent($component)
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑路由')->setConfig(['width' => '500px'])->setContent($component)
);

$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: TP::route()->to([RouteController::class, 'destroy']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.routeGetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);

$table->setAttr([
    'row-key' => 'id'
]);

$table->setPagination(false);


return $table->render();
